package cn.lfzj.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aventrix.jnanoid.jnanoid.NanoIdUtils;

import cn.lfzj.dao.SequenceDao;
import cn.lfzj.model.Sequence;

@Service
public class SequenceService {
	@Autowired
	private SequenceDao sequenceDao;

	/**
	 * 查询全部班次
	 */
	public List<Sequence> findAll() {
		return sequenceDao.findAll();
	}

	/**
	 * 查询未发车班次
	 */
	public List<Sequence> findNotStart(String date, String lineId) {
		return sequenceDao.findNotStart(date, lineId);
	}

	/**
	 * 新增班次
	 */
	public int addSequence(Sequence Sequence) {
		if (sequenceDao.find(Sequence) != null) {
			return 2;
		}
		Sequence.setSequenceId(NanoIdUtils.randomNanoId());
		return sequenceDao.addSequence(Sequence);
	}

	/**
	 * 删除班次
	 */
	public int delSequence(String sequenceId) {
		return sequenceDao.delSequence(sequenceId);
	}

	/**
	 * 修改班次
	 */
	public int updSequence(Sequence sequence) {
		Sequence result = sequenceDao.find(sequence);
		if (result != null && sequence.getSequenceId() != result.getSequenceId()) {
			return -1;
		} else if (result != null && sequence.getSequenceId() == result.getSequenceId()) {
			return -2;
		}
		return sequenceDao.updSequence(sequence);
	}

	/**
	 * 根据ID查班次
	 */
	public Sequence selById(String sequenceId) {
		return sequenceDao.selById(sequenceId);
	}

}
